package com.example.hotelmanagement.service.impl;

import com.example.hotelmanagement.common.pojo.PageResult;
import com.example.hotelmanagement.domain.entity.User;
import com.example.hotelmanagement.domain.vo.LoginReqVo;
import com.example.hotelmanagement.domain.vo.UserPageReqVo;
import com.example.hotelmanagement.mapper.UserMapper;
import com.example.hotelmanagement.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 用户表(User)表服务实现类
 *
 * @author chenhetao
 * @since 2023-06-10 17:32:51
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Long id) {
        return userMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<User> queryByPage(UserPageReqVo user) {
        return userMapper.selectPageByCondition(user);
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        user.setId(null);
        userMapper.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        userMapper.updateById(user);
        return userMapper.selectById(user.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    /**
     * 通过用户名密码判断是否成功登录
     *
     * @param loginReqVo
     * @return 1为成功，0为失败
     */
    @Override
    public Long login(LoginReqVo loginReqVo) {
        User user = new User();
        BeanUtils.copyProperties(loginReqVo,user);
        return userMapper.loginJudge(user);
    }
}
